package pagetest;/**
 * Create by IntelliJ IDEA
 *
 * @author: MaoYongQi
 * @date-Time: 2018/4/23 10:55
 * @description:
 */

import redis.clients.jedis.Jedis;

import java.util.*;

/**
 * Create by IntelliJ IDEA
 * @author: MaoYongQi
 * @date-Time: 2018/4/23 10:55
 * @description:
 */
public class redistest {
    //redis ids集合的 的key名称
    public static   String KEYIDS= "IDS";

    // redis 对象的 的key名称  "SYS:BILL:"+"id"  获取单个对象
    public static   String OBJECTKEYID= "OBJECT:";

    // list 的主键名称
    public static   String MAJORKEY="id";

    //num 当前页数
    public static   Long num = 2l;
    //pageCount 每页条数
    public static   Long pageCount = 10l;
    
    public static final String LOCAL = "47.94.111.174";

    //所有缓存3天后失效
    public static void main(String[] args) {
        //写入数据
        setDateBase();

        //读取列表数据
        readDateBase(num,pageCount);

        //读取数据单个对象
        readObjectDateBase("0");
    }


    /**
     * 读取数据
     */
    private static void readDateBase(Long num,Long pageCount) {
        //获取redis对象
        Jedis jedis = new Jedis(LOCAL);
        try {
            List<Map<String, String>> findPagedQuery = RedisUtil.findPagedQuery(KEYIDS, OBJECTKEYID, num, pageCount, jedis);
            if (findPagedQuery!=null && findPagedQuery.size()>0) {
                System.out.println("查询到当前的数据记录：");
                for (Map<String, String> map : findPagedQuery) {
                    System.out.println(map.toString());
                }
            }else{
                System.out.println("当前页数据为空");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            jedis.close();
        }
    }

    /**
     * 读取数据单个对象
     */
    private static void readObjectDateBase(String key) {
        key= OBJECTKEYID+key;
        //获取redis对象
        Jedis jedis = new Jedis(LOCAL);
        try {
            Map<String, String> hgetAll = jedis.hgetAll(key);
            System.out.println("查询id为："+key+"的对象结果：");
            System.out.println(hgetAll.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            jedis.close();
        }
    }


    /**
     * 存入数据源
     */
    public static void setDateBase(){
        Jedis jedis = new Jedis(LOCAL);
        List<Map<String, Object>> list =new ArrayList<Map<String,Object>>();
        Date d= new Date();
        for (int i = 0; i < 20; i++) {
            Map<String, Object> map =new HashMap<String, Object>();
            map.put(MAJORKEY, i);
            map.put("name", "雄"+i);
            map.put("createDate", d);
            list.add(map);
        }
        try {
            RedisUtil.setCacheObjectInfo(list, KEYIDS, OBJECTKEYID, MAJORKEY, jedis);
            System.out.println("写入数据成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            jedis.close();
        }
    }

}
